/*
 * @Author: 吴世扬 18368095041@163.com
 * @Date: 2024-09-04 17:17:36
 * @LastEditors: 吴世扬 18368095041@163.com
 * @LastEditTime: 2024-10-11 11:15:18
 * @FilePath: /react18-vite-admin/src/store/modules/user.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { create } from 'zustand';
import { useNavigate, useSearchParams } from 'react-router-dom';

import { getAccessToken } from '@/utils';
import { setAccessToken, setMyInfo as setMyInfoStorage } from '@/utils';
import { UserStore } from '@/typings/store/user';
import { readMyInfo } from '@/api/my';
import defaultAvatar from '@/assets/images/common/default-avatar.png';

export const useUserStore = create<UserStore>((set) => {
    return {
        token: getAccessToken(),
        setToken(token) {
            set({ token });
        },
        localUserInfo: undefined,
        name: undefined,
        avatar: defaultAvatar,
        setName(name) {
            set({ name });
        },
        role: undefined,
        roleList: [],
        setRole(role) {
            set({ role });
        },
        setRoleList(roleList) {
            set({ roleList });
        },
        menuTree: [],
        setMenuTree(menuTree) {
            set({ menuTree });
        },
        myInfo: undefined,
        setMyInfo(myInfo) {
            set({ myInfo });
        }
        // login(token: string) {
        //     setNeedUpdate(true);
        //     // location.pathname = '/';
        //     console.log(location);
        // }
        // login(loginData: LoginParam) => {
        //     // const { username, password, captcha } = loginData;
        //     // return loginApi({
        //     //     username: username.trim(),
        //     //     password,
        //     //     captcha
        //     // })
        //     //     .then((res: any) => {
        //     //         setToken(res.data);
        //     //         token.value = res.data;
        //     //         return res;
        //     //     })
        //     //     .catch((error) => {
        //     //         return Promise.reject(error);
        //     //     });
        // };
    };
});

export const useUser = () => {
    const {
        token,
        setToken,
        setRole,
        setRoleList,
        setMenuTree,
        setName,
        setMyInfo,
        menuTree
    } = useUserStore((state) => state);
    const navigate = useNavigate();
    const [searchParams] = useSearchParams();

    const loginByToken = (token: string) => {
        setToken(token);
        setAccessToken(token);
        const redirect = searchParams.get('redirect') || '/';
        navigate(redirect);
    };

    const getMyInfo = () => {
        return readMyInfo().then((res) => {
            const userInfoData = res.data;
            if (userInfoData.roleList && userInfoData.roleList.length > 0) {
                setRoleList(userInfoData.roleList);
                setRole(userInfoData.roleList[0]);
            }
            setMenuTree(userInfoData.menuTree || []);
            setName(userInfoData.username || '佚名');
            // avatar.value = userInfoData.avatar ? `${import.meta.env.VITE_PIC_HOST}/admin/api/file/show/image/${userInfoData.avatar}` : defaultAvatar;
            setMyInfo(userInfoData);
            setMyInfoStorage(userInfoData);
            return res;
        });
    };

    return {
        token,
        loginByToken,
        getMyInfo,
        menuTree,
        setMenuTree
    };
};
